﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.BLL;
using Garlic.CC.BaseClass;
using Garlic.CC.WebUtility;
using Garlic.CC.Utility;

public partial class SystemManagement_RoleList : BasePage
{
    

    RoleManager RoleMgr = null;//= new UserManager(this.CurrentUser);

    #region page load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //WorkshopMgr = new WorkshopManager(this.CurrentUser);
            if (!Page.IsPostBack)
            {
                //Init Page
                //   InitSearchBar();

                //this.Search();
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    #endregion

    #region method
    /// <summary>
    /// 初始化页面
    /// </summary>
    private void InitPage()
    {

    }

    /// <summary>
    /// 初始化查询栏
    /// </summary>
    private void InitSearchBar()
    {
        //初始化查询列表
        // this.GeneralSearchBar1.ListSourcXYbles = WorkshopMgr.GetSearchBarDataSourcXYbles();


    }

    /// <summary>
    /// 当页面重新刷新后，需要调用search方法进行重新查询
    /// </summary>
    protected override void OnReFreshPostBack()
    {
        Search();
    }

    private void DeleteRecords()
    {
        string checkedRecords = "";
        for (int i = 0; i < this.SmartGrid.Rows.Count; i++)
        {
            CheckBox cbSelected = this.SmartGrid.Rows[i].FindControl("checkitem") as CheckBox;
            if (cbSelected != null && cbSelected.Checked)
            {
                checkedRecords += SmartGrid.DataKeys[i].Value + ",";
            }
        }
        if (string.IsNullOrEmpty(checkedRecords)) return;
        checkedRecords = checkedRecords.Substring(0, checkedRecords.Length - 1);

        RoleManager.DeleteMultRecords(checkedRecords);
        this.ShowMessage("删除");
        this.Search();
    }

    private void Search()
    {
        this.SmartGrid.DataSourceID = this.SmartDataViewSource.ID;

        //string condition = this.GeneralSearchBar1.GetConditionStr();
        string condition = "";

        if (searchRoleName.Text.Trim() != "")
            condition += " role_name like '%" + SqlHelper.ReplaceInvalidWords(searchRoleName.Text)+ "%'";
        if (searchRoleType.SelectedValue!="-1")
        {
            if (condition != "")
                condition += " and ";
            condition += " role_type =" + searchRoleType.SelectedValue;
        }
        this.SmartDataViewSource.SelectParameters["textWhere"].DefaultValue = condition;
    }

    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }

    private void ExportExcel()
    {
        string filename = "role";
        string message;
        bool isSuccess = Garlic.Infrustructure.Utilities.WebUtil.Export(false, SmartGrid, ref filename, out message);
        if (isSuccess)
            OutputFilePath = filename;
        else
            Message = message;

    }


    #endregion

    #region toolbar event handler
    /// <summary>
    /// 工具栏事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
        try
        {
            switch (args.Command)
            {
                case ToolbarCommand.Delete:
                    DeleteRecords();
                    break;
                case ToolbarCommand.Search:
                    Search();
                    this.divSearch.Visible = false;
                    break;
                case ToolbarCommand.HideOrExpand:
                    this.divSearch.Visible = !this.divSearch.Visible;
                    break;
                case ToolbarCommand.Export:
                    ExportExcel();
                    break;
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
    #endregion

    protected void SmartGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[6].Text == "0")
                    e.Row.Cells[6].Text = "普通用戶";
                else
                    e.Row.Cells[6].Text = "&nbsp;";
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
}
